//
//  FSTextField.m
//  fanshu
//
//  Created by mh on 16/3/29.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSTextField.h"
@interface FSTextField ()<UITextFieldDelegate>
@property(nonatomic,weak)UIButton *cancleBtn;
@property(nonatomic,weak)UIImageView *searchBar;
@property(nonatomic,weak)UIView *searchBarView;
@end
@implementation FSTextField
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.keyboardType = UIKeyboardTypeNamePhonePad;
        self.returnKeyType = UIReturnKeySearch;
        self.placeholder = @"搜索简书上的内容";
        self.delegate = self;
        
        UIButton *cancleBtn = [[UIButton alloc] init];
        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancleBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        cancleBtn.titleLabel.font = setFont(13.0f);
        [cancleBtn addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
        self.rightView = cancleBtn;
        self.cancleBtn = cancleBtn;
        self.rightViewMode = UITextFieldViewModeAlways;
        
        UIView *searchBarView = [[UIView alloc] init];
        self.leftView = searchBarView;
        self.leftViewMode = UITextFieldViewModeAlways;
        self.searchBarView = searchBarView;
        
        UIImageView *searchBar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_search"]];
        self.searchBar = searchBar;
        [searchBarView addSubview:searchBar];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - 1, ScreenWidth, 1)];
        view.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.1f];
        [self addSubview:view];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat margin = 10;
    
    CGFloat cancleBtnW = [self.cancleBtn.titleLabel.text sizeWithFont:setFont(13.0f)].width;
    CGFloat cancleBtnH = self.height;
    CGFloat cancleBtnX = self.width - cancleBtnW - 10;
    CGFloat cancleBtnY = 0;
    self.cancleBtn.frame = CGRectMake(cancleBtnX, cancleBtnY, cancleBtnW, cancleBtnH);
    
    CGFloat searchBarViewX = margin * 0.6;
    CGFloat searchBarViewY = margin * 1.5;
    CGFloat searchBarViewH = self.height - margin * 3;
    CGFloat searchBarViewW = searchBarViewH + margin;
    self.searchBarView.frame = CGRectMake(searchBarViewX, searchBarViewY, searchBarViewW, searchBarViewH);

    CGFloat searchBarX = 0;
    CGFloat searchBarY = 0;
    CGFloat searchBarH = searchBarViewH;
    CGFloat searchBarW = searchBarH;
    self.searchBar.frame = CGRectMake(searchBarX, searchBarY, searchBarW, searchBarH);
    
}

-(void)dealloc{
    self.searchBar = nil;
}

//取消按钮点击
- (void)cancle{
    if (self.cancleBtnOnClick) {
        self.cancleBtnOnClick();
    }
}

#pragma mark -UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //当键盘 按下return键  的时候调用此方法   返回NO表示忽略本次点击
    if (self.searchBarOnClick) {
        self.searchBarOnClick();
    }
    return YES;
}

@end
